#include "loginwindow.h"
#include "mainwindow.h"
#include <QApplication>


LoginWindow::LoginWindow(Database *db, QWidget *parent)
    : QMainWindow(parent)
    , db(db)
{
    setupUI();
    createConnections();

    if (!db->connect()) {
        QMessageBox::critical(this, "错误", "无法连接到数据库！");
        QApplication::quit();
    }
}

LoginWindow::~LoginWindow()
{
}

void LoginWindow::setupUI()
{
    setWindowTitle("图书管理系统 - 登录");
    resize(400, 300);

    centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    mainLayout = new QVBoxLayout(centralWidget);
    mainLayout->setSpacing(20);
    mainLayout->setContentsMargins(40, 40, 40, 40);

    // 标题
    QLabel *titleLabel = new QLabel("图书管理系统", this);
    titleLabel->setStyleSheet("font-size: 24px; font-weight: bold; color: #2196F3;");
    titleLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(titleLabel);

    // 用户名输入
    QLabel *usernameLabel = new QLabel("用户名:", this);
    usernameEdit = new QLineEdit(this);
    usernameEdit->setPlaceholderText("请输入用户名");
    usernameEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(usernameLabel);
    mainLayout->addWidget(usernameEdit);

    // 密码输入
    QLabel *passwordLabel = new QLabel("密码:", this);
    passwordEdit = new QLineEdit(this);
    passwordEdit->setPlaceholderText("请输入密码");
    passwordEdit->setEchoMode(QLineEdit::Password);
    passwordEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(passwordLabel);
    mainLayout->addWidget(passwordEdit);

    // 按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(20);

    loginButton = new QPushButton("登录", this);
    loginButton->setStyleSheet("QPushButton { background-color: #2196F3; color: white; padding: 8px 16px; "
                             "border: none; border-radius: 4px; }"
                             "QPushButton:hover { background-color: #1976D2; }"
                             "QPushButton:pressed { background-color: #0D47A1; }");

    registerButton = new QPushButton("注册", this);
    registerButton->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; padding: 8px 16px; "
                                "border: none; border-radius: 4px; }"
                                "QPushButton:hover { background-color: #388E3C; }"
                                "QPushButton:pressed { background-color: #1B5E20; }");

    buttonLayout->addWidget(loginButton);
    buttonLayout->addWidget(registerButton);
    mainLayout->addLayout(buttonLayout);

    // 设置窗口样式
    setStyleSheet("QMainWindow { background-color: white; }");
}

void LoginWindow::createConnections()
{
    connect(loginButton, &QPushButton::clicked, this, &LoginWindow::onLoginClicked);
    connect(registerButton, &QPushButton::clicked, this, &LoginWindow::onRegisterClicked);
}

void LoginWindow::onLoginClicked()
{
    QString username = usernameEdit->text().trimmed();
    QString password = passwordEdit->text().trimmed();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "警告", "用户名和密码不能为空！");
        return;
    }

    if (db->validateUser(username, password)) {
        MainWindow *mainWindow = new MainWindow(username, db);
        mainWindow->show();
        this->close();
    } else {
        QMessageBox::warning(this, "错误", "用户名或密码错误！");
    }
}

void LoginWindow::onRegisterClicked()
{
    QString username = usernameEdit->text().trimmed();
    QString password = passwordEdit->text().trimmed();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "警告", "用户名和密码不能为空！");
        return;
    }

    if (db->addUser(username, password)) {
        QMessageBox::information(this, "成功", "注册成功！请登录。");
    } else {
        QMessageBox::warning(this, "错误", "注册失败！用户名可能已存在。");
    }
} 
